package impl

import (
	"context"
	compb "gitee.com/micro-services/protocol/common"
	pb "gitee.com/micro-services/protocol/tag/server"
	"micro.tag.server/do"
	"micro.tag.server/models"
	"strings"
	"time"
)

const (
	Success = "success"
)

func CreateHotelTag(ctx context.Context, r *pb.CreateHotelTagReq) (*pb.CreateHotelTagRsp, error) {
	tag := &models.VsHTags{
		HotelId:     r.HotelID,
		TagName:     strings.Join(r.Tag, ","),
		CreatedTime: time.Now().Unix(),
		UpdatedTime: time.Now().Unix(),
	}
	err := do.CreateHotelTag(tag)
	if err != nil {
		return nil, err
	}

	return &pb.CreateHotelTagRsp{
		RetCode: 0,
		Msg:     Success,
	}, nil
}

func DeleteHotelTag(ctx context.Context, r *pb.DeleteHotelTagReq) (*pb.DeleteHotelTagRsp, error) {
	err := do.DeleteHotelTag(r.HotelID, r.TagID)
	if err != nil {
		return nil, err
	}

	return &pb.DeleteHotelTagRsp{
		RetCode: 0,
		Msg:     Success,
	}, nil
}

func HotelTags(ctx context.Context, r *pb.HotelTagsReq) ([]*compb.HotelTagInfo, error) {
	tags, err := do.HotelTags(r.HotelID)
	if err != nil {
		return nil, err
	}

	infos := make([]*compb.HotelTagInfo, 0)
	for _, v := range tags {
		infos = append(infos, &compb.HotelTagInfo{
			TagID:   v.Id,
			HotelID: v.HotelId,
			TagName: strings.Split(v.TagName, ","),
		})
	}

	return infos, nil
}
